﻿using NDK.Communacation;
using NDK.Module.Model;
using NDK.Motion.Card;
using NDK.Motion.MachineManager;
using NDK.Motion.MachineResources;
//using CSharp_OPTControllerAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using NDK.Module.Model.Quebec;
using NDK.LocalUtils;
using TwinCAT.Ads.TypeSystem;
using HslCommunication.Profinet.Omron;
using HslCommunication;
//using motormaster;

namespace NDK.Motion.BLL
{
    public static class Machine
    {
        #region DI
        public static InputSetting di_急停按钮1;
        public static InputSetting di_防撞光电;
        public static InputSetting di_PPG工位1光电;
        public static InputSetting di_PPG工位2光电;
        public static InputSetting di_扫码平台真空1反馈;
        public static InputSetting di_扫码平台真空2反馈;
        public static InputSetting di_双动子平台1气缸1原点;
        public static InputSetting di_双动子平台1气缸1动点;
        public static InputSetting di_双动子平台1气缸2原点;
        public static InputSetting di_双动子平台1气缸2动点;
        public static InputSetting di_双动子平台2气缸1原点;
        public static InputSetting di_双动子平台2气缸1动点;
        public static InputSetting di_双动子平台2气缸2原点;
        public static InputSetting di_双动子平台2气缸2动点;
        public static InputSetting di_移栽工位气缸原点;
        public static InputSetting di_移栽工位气缸动点;
        public static InputSetting di_移栽工位真空1反馈;
        public static InputSetting di_移栽工位真空2反馈;
        public static InputSetting di_移栽工位叠片光电;
        public static InputSetting di_翻转平台真空1反馈;
        public static InputSetting di_翻转平台真空2反馈;
        public static InputSetting di_翻转平台真空3反馈;
        public static InputSetting di_翻转平台真空4反馈;
        public static InputSetting di_上料操作侧门1关闭;
        public static InputSetting di_上料操作侧门2关闭;
        public static InputSetting di_上料Tray侧门1关闭;
        [MachinePropAttr("ATL")]
        public static InputSetting di_上料Tray侧门2关闭;
        public static InputSetting di_启动按钮;
        public static InputSetting di_停止按钮;
        public static InputSetting di_复位按钮;
        public static InputSetting di_手自动按钮;
        public static InputSetting di_下料操作侧门1关闭;
        public static InputSetting di_下料操作侧门2关闭;

        [MachinePropAttr("ATL")]
        public static InputSetting di_下料操作侧门3关闭;
        public static InputSetting di_急停按钮2;
        public static InputSetting di_急停按钮3;
        public static InputSetting di_急停按钮4;
        public static InputSetting di_检测平台真空1反馈;
        public static InputSetting di_检测平台真空2反馈;
        public static InputSetting di_下料翻转真空1反馈;
        public static InputSetting di_下料翻转真空2反馈;
        public static InputSetting di_下料翻转气缸原点;
        public static InputSetting di_下料翻转气缸动点;
        public static InputSetting di_下料翻转光电;
        public static InputSetting di_上料平台1真空1反馈;
        public static InputSetting di_上料平台1真空2反馈;
        public static InputSetting di_上料平台2真空1反馈;
        public static InputSetting di_上料平台2真空2反馈;
        public static InputSetting di_备用5;
        public static InputSetting di_备用6;
        public static InputSetting di_备用7;
        public static InputSetting di_备用8;
        public static InputSetting di_备用9;
        public static InputSetting di_备用10;
        #endregion

        #region DO 
        public static OutputSetting do_扫码平台真空1吸;
        public static OutputSetting do_扫码平台真空1破;
        public static OutputSetting do_扫码平台真空2吸;
        public static OutputSetting do_扫码平台真空2破;
        public static OutputSetting do_双动子平台1气缸1缩回;
        public static OutputSetting do_双动子平台1气缸1伸出;
        public static OutputSetting do_双动子平台1气缸2缩回;
        public static OutputSetting do_双动子平台1气缸2伸出;
        public static OutputSetting do_双动子平台2气缸1缩回;
        public static OutputSetting do_双动子平台2气缸1伸出;
        public static OutputSetting do_双动子平台2气缸2缩回;
        public static OutputSetting do_双动子平台2气缸2伸出;
        public static OutputSetting do_PPG工位平台1通断阀1;
        public static OutputSetting do_PPG工位平台1通断阀2;
        public static OutputSetting do_PPG工位平台2通断阀1;
        public static OutputSetting do_PPG工位平台2通断阀2;
        public static OutputSetting do_移栽工位气缸缩回;
        public static OutputSetting do_移栽工位气缸伸出;
        public static OutputSetting do_移栽工位真空1吸;
        public static OutputSetting do_移栽工位真空1破;
        public static OutputSetting do_移栽工位真空2吸;
        public static OutputSetting do_移栽工位真空2破;
        public static OutputSetting do_翻转平台真空1吸;
        public static OutputSetting do_翻转平台真空1破;
        public static OutputSetting do_翻转平台真空2吸;
        public static OutputSetting do_翻转平台真空2破;
        [MachinePropAttr("ATL")]
        public static OutputSetting do_翻转平台真空3吸;
        [MachinePropAttr("ATL")]
        public static OutputSetting do_翻转平台真空3破;
        [MachinePropAttr("ATL")]
        public static OutputSetting do_翻转平台真空4吸;
        [MachinePropAttr("ATL")]
        public static OutputSetting do_翻转平台真空4破;
        public static OutputSetting do_上料操作侧门1锁定;
        public static OutputSetting do_上料操作侧门2锁定;
        public static OutputSetting do_上料Tray侧门1锁定;
        [MachinePropAttr("ATL")]
        public static OutputSetting do_上料Tray侧门2锁定;
        public static OutputSetting do_启动按钮灯;
        public static OutputSetting do_停止按钮灯;
        public static OutputSetting do_复位按钮灯;
        public static OutputSetting do_红灯;
        public static OutputSetting do_黄灯;
        public static OutputSetting do_绿灯;
        public static OutputSetting do_蜂鸣器;
        public static OutputSetting do_下料操作侧门1锁定;
        public static OutputSetting do_下料操作侧门2锁定;
        [MachinePropAttr("ATL")]
        public static OutputSetting do_下料操作侧门3锁定;
        public static OutputSetting do_检测平台真空1吸;
        public static OutputSetting do_检测平台真空1破;
        public static OutputSetting do_检测平台真空2吸;
        public static OutputSetting do_检测平台真空2破;
        public static OutputSetting do_下料翻转真空1吸;
        public static OutputSetting do_下料翻转真空1破;
        public static OutputSetting do_下料翻转真空2吸;
        public static OutputSetting do_下料翻转真空2破;
        public static OutputSetting do_下料翻转气缸原点位;
        public static OutputSetting do_下料翻转气缸动点位;
        public static OutputSetting do_上料平台1真空1吸;
        public static OutputSetting do_上料平台1真空1破;
        public static OutputSetting do_上料平台1真空2吸;
        public static OutputSetting do_上料平台1真空2破;
        public static OutputSetting do_上料平台2真空1吸;
        public static OutputSetting do_上料平台2真空1破;
        public static OutputSetting do_上料平台2真空2吸;
        public static OutputSetting do_上料平台2真空2破;
        public static OutputSetting do_备用9;
        public static OutputSetting do_备用10;
        #endregion

        #region 气缸
        public static DoubleDriveCylinder cy_双动子平台1气缸1 = null;
        public static DoubleDriveCylinder cy_双动子平台1气缸2 = null; 
        public static DoubleDriveCylinder cy_双动子平台2气缸1 = null;
        public static DoubleDriveCylinder cy_双动子平台2气缸2 = null;
        public static DoubleDriveCylinder cy_移栽工位气缸 = null;
        public static DoubleDriveCylinder cy_下料翻转气缸 = null;
        #endregion

        #region PPG实例和PLC实例
        public static KeyencePPG PPG1 = new KeyencePPG();
        public static KeyencePPG PPG2 = new KeyencePPG();

        public static OmronCipNet OmronPLC = new OmronCipNet("192.168.3.10");
        public static OperateResult opreateResult_Isconnect = new OperateResult();

        public static OperateResult opreateResult = new OperateResult();
        #endregion

        //Card
        public static Adlink AdlinkCard = new Adlink();
        //

        #region 光源
        public static ImageLights imageLights_上料仓Tray盘拍照;

        public static ImageLights imageLights_产品1定位拍照;
        public static ImageLights imageLights_产品2定位拍照;

        public static ImageLights imageLights_量测平台1拍照1;
        public static ImageLights imageLights_量测平台1拍照2;
        public static ImageLights imageLights_量测平台1拍照3;

        public static ImageLights imageLights_量测平台2拍照1;
        public static ImageLights imageLights_量测平台2拍照2;
        public static ImageLights imageLights_量测平台2拍照3;

        public static ImageLights imageLights_下料仓Tray盘拍照;

        public static ImageLights imageLights_量测平台1拍照11;
        #endregion

        #region 点位
        //上料平台
        public static Position pos_Load_初始位 = null;
        public static Position pos_Load_扫码位 = null;
        public static Position pos_Load_接料位 = null;

        //双动子平台1
        public static Position pos_DoubleMotor_Panel1_初始位 = null;
        public static Position pos_DoubleMotor_Panel1_取料位 = null;
        public static Position pos_DoubleMotor_Panel1_放料位 = null;
        public static Position pos_DoubleMotor_Panel1_穴1放穴2 = null;
        public static Position pos_DoubleMotor_Panel1_穴2放穴1 = null;

        public static Position pos_DoubleMotor_Panel2_初始位 = null;
        public static Position pos_DoubleMotor_Panel2_取料位 = null;
        public static Position pos_DoubleMotor_Panel2_放料位 = null;

        //PPG工位
        public static Position pos_PPG_初始位 = null;
        public static Position pos_PPG_测量位 = null;
        public static Position pos_PPG_过渡位 = null;

        //翻转平台
        public static Position pos_Rotate_初始位 = null;
        public static Position pos_Rotate_翻转位 = null;
        [MachinePropAttr("DSY")]
        public static Position pos_Rotate_下料位 = null;
        
        //移栽工位
        public static Position pos_TransMove_初始位 = null;
        public static Position pos_TransMove_取料位 = null;
        public static Position pos_TransMove_放料位 = null;
        public static Position pos_TransMove_激光扫描起始位 = null;
        public static Position pos_TransMove_激光扫描终点位 = null;
        //测激光工站
        public static Position pos_SideLaser_初始位 = null;
        public static Position pos_SideLaser_长边扫描位 = null;
        public static Position pos_SideLaser_短边扫描位 = null;

        //量测工位
        public static Position pos_Measure_初始位 = null;
        public static Position pos_Measure_平台1长边扫描起始位 = null;
        public static Position pos_Measure_平台1长边扫描终点位 = null;
        public static Position pos_Measure_平台1短边扫描起始位 = null;
        public static Position pos_Measure_平台1短边扫描终点位 = null;

        public static Position pos_Measure_平台2长边扫描起始位 = null;
        public static Position pos_Measure_平台2长边扫描终点位 = null;
        public static Position pos_Measure_平台2短边扫描起始位 = null;
        public static Position pos_Measure_平台2短边扫描终点位 = null;

        public static Position pos_Measure_平台1拍照位 = null;
        public static Position pos_Measure_平台2拍照位 = null;
        public static Position pos_Measure_下料位 = null;
        public static Position pos_Measure_返回避让位 = null;

        //相机工位
        public static Position pos_Camera_初始位置 = null;
        public static Position pos_Camera_拍照位 = null;

        //下料平台
        public static Position pos_Unloading_初始位 = null;
        public static Position pos_Unloading_取料位 = null;
        public static Position pos_Unloading_放料位 = null;
        #endregion


        public static bool ImageLightInit(string ProjectName)
        {
            if (MachineDataManager.Instance.ProjectImageLights.Count == 0)
                return false;
            var findItem = MachineDataManager.Instance.ProjectImageLights.Find(x => x.Name.Equals(ProjectName));
            List<ImageLights> CurProjectImageLightslist;
            if (findItem != null)
                CurProjectImageLightslist = findItem.ImageLightsList;
            else
                return false;

            #region 光源
            imageLights_上料仓Tray盘拍照 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "上料仓Tray盘拍照");
            imageLights_产品1定位拍照 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "产品1定位拍照");

            imageLights_产品2定位拍照 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "产品2定位拍照");
            imageLights_量测平台1拍照1 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "量测平台1拍照1");
            imageLights_量测平台1拍照2 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "量测平台1拍照2");
            imageLights_量测平台1拍照3 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "量测平台1拍照3");

            imageLights_量测平台2拍照1 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "量测平台2拍照1");
            imageLights_量测平台2拍照2 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "量测平台2拍照2");
            imageLights_量测平台2拍照3 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "量测平台2拍照3");
            imageLights_下料仓Tray盘拍照 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "下料仓Tray盘拍照");
            imageLights_量测平台1拍照11 = CurProjectImageLightslist.SingleOrDefault(i => i.ImageName == "量测平台1拍照11");
            #endregion

            Common.AppendUiLog.Log($"当前光源Source为{ProjectName}", BZ.Logger.LogType.Alert);
            return true;
        }


        /// <summary>
        /// 资源初始化
        /// </summary>
        /// <returns></returns>

        public static void MachineResourceInit()
        {
            try
            {
                #region IO初始化
                #region DI
                try
                {
                    di_急停按钮1 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "急停按钮1");
                    di_防撞光电 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "防撞光电");

                    di_PPG工位1光电 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "PPG工位1光电");
                    di_PPG工位2光电 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "PPG工位2光电");
                    di_扫码平台真空1反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "扫码平台真空1反馈");
                    di_扫码平台真空2反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "扫码平台真空2反馈");

                    di_双动子平台1气缸1原点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "双动子平台1气缸1原点");
                    di_双动子平台1气缸1动点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "双动子平台1气缸1动点");
                    di_双动子平台1气缸2原点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "双动子平台1气缸2原点");
                    di_双动子平台1气缸2动点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "双动子平台1气缸2动点");
                    di_双动子平台2气缸1原点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "双动子平台2气缸1原点");
                    di_双动子平台2气缸1动点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "双动子平台2气缸1动点");
                    di_双动子平台2气缸2原点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "双动子平台2气缸2原点");

                    di_双动子平台2气缸2动点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "双动子平台2气缸2动点");
                    di_移栽工位气缸原点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "移栽工位气缸原点");

                    di_移栽工位气缸动点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "移栽工位气缸动点");
                    di_移栽工位真空1反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "移栽工位真空1反馈");
                    di_移栽工位真空2反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "移栽工位真空2反馈");
                    di_移栽工位叠片光电 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "移栽工位叠片光电");

                    di_翻转平台真空1反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "翻转平台真空1反馈");
                    di_翻转平台真空2反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "翻转平台真空2反馈");
                    di_翻转平台真空3反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "翻转平台真空3反馈");
                    di_翻转平台真空4反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "翻转平台真空4反馈");

                    di_上料操作侧门1关闭 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "上料操作侧门1关闭");
                    di_上料操作侧门2关闭 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "上料操作侧门2关闭");
                    di_上料Tray侧门1关闭 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "上料Tray侧门1关闭");

                    if (Common.Line.Equals(Common.LineName.ATL.ToString()))
                        di_上料Tray侧门2关闭 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "上料Tray侧门2关闭");
                    di_启动按钮 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "启动按钮");

                    di_停止按钮 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "停止按钮");
                    di_复位按钮 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "复位按钮");
                    di_手自动按钮 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "手自动按钮");
                    di_下料操作侧门1关闭 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "下料操作侧门1关闭");
                    di_下料操作侧门2关闭 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "下料操作侧门2关闭");

                    if(Common.Line.Equals(Common.LineName.ATL.ToString()))
                    {
                        di_下料操作侧门3关闭 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "下料操作侧门3关闭");
                    }

                    di_急停按钮2 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "急停按钮2");
                    di_急停按钮3 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "急停按钮2");
                    di_急停按钮4 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "急停按钮4");

                    di_检测平台真空1反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "检测平台真空1反馈");
                    di_检测平台真空2反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "检测平台真空2反馈");

                    di_下料翻转真空1反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "下料翻转真空1反馈");
                    di_下料翻转真空2反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "下料翻转真空2反馈");

                    di_下料翻转气缸原点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "下料翻转气缸原点");
                    di_下料翻转气缸动点 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "下料翻转气缸动点");

                    di_下料翻转光电 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "下料翻转光电");

                    di_上料平台1真空1反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "上料平台1真空1反馈");
                    di_上料平台1真空2反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "上料平台1真空2反馈");
                    di_上料平台2真空1反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "上料平台2真空1反馈");
                    di_上料平台2真空2反馈 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "上料平台2真空2反馈");
                    di_备用5 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "备用5");
                    di_备用6 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "备用6");
                    di_备用7 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "备用7");
                    di_备用8 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "备用8");
                    di_备用9 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "备用9");
                    di_备用10 = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == "备用10");
                }
                catch { }
                #endregion

                #region DO
                try
                {
                    do_扫码平台真空1吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "扫码平台真空1吸");
                    do_扫码平台真空1破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "扫码平台真空1破");
                    do_扫码平台真空2吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "扫码平台真空2吸");
                    do_扫码平台真空2破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "扫码平台真空2破");

                    do_双动子平台1气缸1缩回 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "双动子平台1气缸1缩回");
                    do_双动子平台1气缸1伸出 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "双动子平台1气缸1伸出");
                    do_双动子平台1气缸2缩回 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "双动子平台1气缸2缩回");
                    do_双动子平台1气缸2伸出 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "双动子平台1气缸2伸出");
                    do_双动子平台2气缸1缩回 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "双动子平台2气缸1缩回");
                    do_双动子平台2气缸1伸出 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "双动子平台2气缸1伸出");
                    do_双动子平台2气缸2缩回 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "双动子平台2气缸2缩回");
                    do_双动子平台2气缸2伸出 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "双动子平台2气缸2伸出");

                    do_PPG工位平台1通断阀1 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "PPG工位平台1通断阀1");
                    do_PPG工位平台1通断阀2 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "PPG工位平台1通断阀2");
                    do_PPG工位平台2通断阀1 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "PPG工位平台2通断阀1");  
                    do_PPG工位平台2通断阀2 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "PPG工位平台2通断阀2");

                    do_移栽工位气缸缩回 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "移栽工位气缸缩回");
                    do_移栽工位气缸伸出 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "移栽工位气缸伸出");
                    do_移栽工位真空1吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "移栽工位真空1吸");
                    do_移栽工位真空1破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "移栽工位真空1破");
                    do_移栽工位真空2吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "移栽工位真空2吸");
                    do_移栽工位真空2破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "移栽工位真空2破");

                    do_翻转平台真空1吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "翻转平台真空1吸");
                    do_翻转平台真空1破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "翻转平台真空1破");
                    do_翻转平台真空2吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "翻转平台真空2吸");
                    do_翻转平台真空2破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "翻转平台真空2破");
                    if (Common.Line.Equals(Common.LineName.ATL.ToString()))
                        do_翻转平台真空3吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "翻转平台真空3吸");
                    if (Common.Line.Equals(Common.LineName.ATL.ToString()))
                        do_翻转平台真空3破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "翻转平台真空3破");
                    if (Common.Line.Equals(Common.LineName.ATL.ToString()))
                        do_翻转平台真空4吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "翻转平台真空4吸");
                    if (Common.Line.Equals(Common.LineName.ATL.ToString()))
                        do_翻转平台真空4破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "翻转平台真空4破");

                    do_上料操作侧门1锁定 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料操作侧门1锁定");
                    do_上料操作侧门2锁定 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料操作侧门2锁定");
                    do_上料Tray侧门1锁定 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料Tray侧门1锁定");
                    if (Common.Line.Equals(Common.LineName.ATL.ToString()))
                        do_上料Tray侧门2锁定 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料Tray侧门2锁定");

                    do_启动按钮灯 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "启动按钮灯");
                    do_停止按钮灯 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "停止按钮灯");
                    do_复位按钮灯 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "复位按钮灯");

                    do_红灯 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "红灯");
                    do_黄灯 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "黄灯");
                    do_绿灯 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "绿灯");
                    do_蜂鸣器 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "蜂鸣器");

                    do_下料操作侧门1锁定 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "下料操作侧门1锁定");
                    do_下料操作侧门2锁定 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "下料操作侧门2锁定");
                    if (Common.Line.Equals(Common.LineName.ATL.ToString()))
                        do_下料操作侧门3锁定 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "下料操作侧门3锁定");

                    do_检测平台真空1吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "检测平台真空1吸");
                    do_检测平台真空1破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "检测平台真空1破");
                    do_检测平台真空2吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "检测平台真空2吸");
                    do_检测平台真空2破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "检测平台真空2破");

                    do_下料翻转真空1吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "下料翻转真空1吸");
                    do_下料翻转真空1破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "下料翻转真空1破");
                    do_下料翻转真空2吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "下料翻转真空2吸");
                    do_下料翻转真空2破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "下料翻转真空2破");

                    do_下料翻转气缸原点位 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "下料翻转气缸原点位");
                    do_下料翻转气缸动点位 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "下料翻转气缸动点位");

                    do_上料平台1真空1吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料平台1真空1吸");
                    do_上料平台1真空1破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料平台1真空1破");
                    do_上料平台1真空2吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料平台1真空2吸");
                    do_上料平台1真空2破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料平台1真空2破");
                    do_上料平台2真空1吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料平台2真空1吸");
                    do_上料平台2真空1破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料平台2真空1破");
                    do_上料平台2真空2吸 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料平台2真空2吸");
                    do_上料平台2真空2破 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "上料平台2真空2破");
                    do_备用9 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "备用9");
                    do_备用10 = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == "备用10");
                }
                catch { }
                #endregion
                #endregion

                #region 点位初始化
                //点位初始化
                pos_Load_初始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "上料平台" && p.PositionName == "初始位");
                pos_Load_扫码位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "上料平台" && p.PositionName == "扫码位");
                pos_Load_接料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "上料平台" && p.PositionName == "接料位");

                pos_DoubleMotor_Panel1_初始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "双动子平台1" && p.PositionName == "初始位");
                pos_DoubleMotor_Panel1_取料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "双动子平台1" && p.PositionName == "取料位");
                pos_DoubleMotor_Panel1_放料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "双动子平台1" && p.PositionName == "放料位");
                pos_DoubleMotor_Panel1_穴1放穴2 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "双动子平台1" && p.PositionName == "穴1放穴2");
                pos_DoubleMotor_Panel1_穴2放穴1 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "双动子平台1" && p.PositionName == "穴2放穴1");


                pos_DoubleMotor_Panel2_初始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "双动子平台2" && p.PositionName == "初始位");
                pos_DoubleMotor_Panel2_取料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "双动子平台2" && p.PositionName == "取料位");
                pos_DoubleMotor_Panel2_放料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "双动子平台2" && p.PositionName == "放料位");

                pos_PPG_初始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "PPG工位" && p.PositionName == "初始位");
                pos_PPG_测量位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "PPG工位" && p.PositionName == "测量位");
                pos_PPG_过渡位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "PPG工位" && p.PositionName == "过渡位");
                pos_Rotate_初始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "产品翻转平台" && p.PositionName == "初始位");
                pos_Rotate_翻转位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "产品翻转平台" && p.PositionName == "翻转位");
                pos_Rotate_下料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "产品翻转平台" && p.PositionName == "下料位");

                pos_TransMove_初始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "移栽工位" && p.PositionName == "初始位");
                pos_TransMove_取料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "移栽工位" && p.PositionName == "取料位");
                pos_TransMove_放料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "移栽工位" && p.PositionName == "放料位");

                pos_TransMove_激光扫描起始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "移栽工位" && p.PositionName == "激光扫描起始位");
                pos_TransMove_激光扫描终点位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "移栽工位" && p.PositionName == "激光扫描终点位");

                pos_SideLaser_初始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "侧激光工站" && p.PositionName == "初始位");
                pos_SideLaser_长边扫描位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "侧激光工站" && p.PositionName == "长边扫描位");
                pos_SideLaser_短边扫描位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "侧激光工站" && p.PositionName == "短边扫描位");
                
                pos_Measure_初始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "初始位");
                pos_Measure_平台1长边扫描起始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "平台1长边扫描起始位");
                pos_Measure_平台1长边扫描终点位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "平台1长边扫描终点位");
                pos_Measure_平台1短边扫描起始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "平台1短边扫描起始位");
                pos_Measure_平台1短边扫描终点位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "平台1短边扫描终点位");
                pos_Measure_平台2长边扫描起始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "平台2长边扫描起始位");
                pos_Measure_平台2长边扫描终点位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "平台2长边扫描终点位");
                pos_Measure_平台2短边扫描起始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "平台2短边扫描起始位");
                pos_Measure_平台2短边扫描终点位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "平台2短边扫描终点位");
                pos_Measure_平台1拍照位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "平台1拍照位");
                pos_Measure_平台2拍照位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "平台2拍照位");
                pos_Measure_下料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "下料位");
                pos_Measure_返回避让位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "量测工位" && p.PositionName == "返回避让位");
                
                pos_Camera_初始位置 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "相机Z轴" && p.PositionName == "初始位置");
                pos_Camera_拍照位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "相机Z轴" && p.PositionName == "拍照位");

                pos_Unloading_初始位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "下料平台" && p.PositionName == "初始位");
                pos_Unloading_取料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "下料平台" && p.PositionName == "取料位");
                pos_Unloading_放料位 = MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.Station == "下料平台" && p.PositionName == "放料位");
                #endregion

                #region 光源
                //OPTLightController = MachineDataManager.Instance.LightControllerList.SingleOrDefault(lc => lc.Name == "OPT");

                ImageLightInit(Common.CurProType);
                #endregion

                #region 气缸
                cy_双动子平台1气缸1 = (DoubleDriveCylinder)MachineDataManager.Instance.CylinderList.SingleOrDefault(c => c.Name == "双动子平台1气缸1");
                cy_双动子平台1气缸2 = (DoubleDriveCylinder)MachineDataManager.Instance.CylinderList.SingleOrDefault(c => c.Name == "双动子平台1气缸2");
                cy_双动子平台2气缸1 = (DoubleDriveCylinder)MachineDataManager.Instance.CylinderList.SingleOrDefault(c => c.Name == "双动子平台2气缸1");
                cy_双动子平台2气缸2 = (DoubleDriveCylinder)MachineDataManager.Instance.CylinderList.SingleOrDefault(c => c.Name == "双动子平台2气缸2");
                cy_移栽工位气缸 = (DoubleDriveCylinder)MachineDataManager.Instance.CylinderList.SingleOrDefault(c => c.Name == "移栽工位气缸");
                cy_下料翻转气缸 = (DoubleDriveCylinder)MachineDataManager.Instance.CylinderList.SingleOrDefault(c => c.Name == "下料翻转气缸");
                #endregion

                #region 未赋值对象判断
                List<string> 未成功赋值对象 = new List<string>();
                Type type = typeof(Machine);
                var Fields = type.GetFields();
                foreach (var item in Fields)
                {
                    var attr = item.GetCustomAttribute<MachinePropAttr>();
                    if (attr != null && attr.Line != Common.Line)
                    {
                        continue;
                    }
                    if (item.FieldType == typeof(InputSetting))
                    {
                        InputSetting input = (InputSetting)item.GetValue(null);
                        if (input == null)
                        {
                            未成功赋值对象.Add(item.Name);
                        }
                    }
                    else if (item.FieldType == typeof(OutputSetting))
                    {
                        OutputSetting ouput = (OutputSetting)item.GetValue(null);
                        if (ouput == null)
                        {
                            未成功赋值对象.Add(item.Name);
                        }
                    }
                    else if (item.FieldType == typeof(Position))
                    {
                        Position position = (Position)item.GetValue(null);
                        if (position == null)
                        {
                            未成功赋值对象.Add(item.Name);
                        }
                    }
                    else if (item.FieldType == typeof(ImageLights))
                    {
                        ImageLights imgLights = (ImageLights)item.GetValue(null);
                        if (imgLights == null)
                        {
                            未成功赋值对象.Add(item.Name);
                        }
                    }
                }
                StringBuilder sb = new StringBuilder();
                foreach (var item in 未成功赋值对象)
                {
                    sb.Append(item);
                    sb.Append(",");
                }
                if (sb.Length > 0)
                    sb.Remove(sb.Length - 1, 1);
                if (sb.Length > 0)
                    Common.AppendUiLog.Log($"未成功赋值：{sb}");
                #endregion
            }
            catch (Exception ex)
            {

            }
        }
    }
}